const Mock = require('mockjs')
const { list: listAppPackages } = require('./app-packages')

const AppPackageItems = []

const packagesPage = listAppPackages(1, 50)
const relativePaths = ['aa/test.dll', 'bb/u.exe', 'main.exe']
const fileNames = ['test.dll', 'u.exe', 'main.exe']
for (let i = 0; i < packagesPage.data.records.length; ++i) {
  for (let j = 1; j <= 3; ++j) {
    AppPackageItems.push(Mock.mock({
      id: i * 3 + j,
      appPackageId: packagesPage.data.records[i].id,
      relativePath: relativePaths[j - 1],
      fileName: fileNames[j - 1],
      digest: Mock.Random.uuid()
    }))
  }
}

function add(item) {
  const r = JSON.parse(JSON.stringify(item))
  r.id = AppPackageItems[AppPackageItems.length - 1].id + 1
  AppPackageItems.push(r)
  return r.id
}

function packageItems(appPackageId) {
  appPackageId = appPackageId ? parseInt(appPackageId) : null
  const arr = []
  for (let i = 0; i < AppPackageItems.length; ++i) {
    if (AppPackageItems[i].appPackageId === appPackageId) {
      arr.push(AppPackageItems[i])
    }
  }
  return arr
}

module.exports = { packageItems, add }
